﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Elderos.AI;
using Encog.Neural.Networks;

namespace AccordTrainer
{
    public class GroupQualityComputer : ThresholdQualityComputer
    {
        public override Quality ComputeQuality(TrainItem<ItemInfo>[] trainItems, double threshold)
        {
            var quality = new Quality();

            var grouping = trainItems.GroupBy(x => x.AdditionalInfo.PositionID);

            int truePositive = 0;
            int trueNegative = 0;
            int falsePositive = 0;
            int falseNegative = 0;

            foreach (var group in grouping)
            {
                TrainItem<ItemInfo> winner =
                    group.OrderByDescending(x => x.AdditionalInfo.RealWeight).First();
                bool mustWin = group.Any(x => x.Output > 0.5);

                //должен выиграть
                if (mustWin)
                {
                    //выиграл
                    if (winner.AdditionalInfo.RealWeight > threshold)
                    {
                        if (winner.Output > 0.5)
                            truePositive++;
                        else
                        {
                            falsePositive++;
                            falseNegative++;
                        }
                    }
                    else
                        falseNegative++;
                }
                else
                {
                    if (winner.AdditionalInfo.RealWeight > threshold)
                        falsePositive++;
                    else
                        trueNegative++;
                }
            }
            quality.TruePositive = truePositive;
            quality.TrueNegative = trueNegative;
            quality.FalsePositive = falsePositive;
            quality.FalseNegative = falseNegative;

            quality.Threshold = threshold;

            return quality;
        }
    }
}
